
package com.eascs.web.o2o.scheduler.history;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.web.o2o.scheduler.history.BpPerformanceService;
import com.eascs.web.o2o.scheduler.history.ReportHisTask;
import com.eascs.web.o2o.scheduler.task.ReportTask;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import java.io.FileNotFoundException;

/**
 * 全国各省区品牌贷投放情况
 */
@RestController
public class BpPerformanceController extends BaseRestController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	ReportTask reportTask;

	@Inject
	ReportHisTask reportHisTask;
	
	@Inject
	BpPerformanceService bpPerformanceService;

	/**
	 * 初始化品牌贷指标数据
	 */
	@RequestMapping(value = "/bp/target/init")
	public void initTarget() {
		StopWatch stopWatch = new StopWatch();
		bpPerformanceService.initBpPerformanceTarget();
		logger.info("[BP]初始化品牌贷指标数据结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * 根据传入月份参数初始化月度task
	 */
	@RequestMapping(value = "/bp/task/trigger/months")
	public void triggerMonthTaskByMonths(String months) {
		StopWatch stopWatch = new StopWatch();
		if(StringUtil.isBlank(months)) {
			logger.error("传入月份为空，任务结束");
			return;
		}

		String[] monthArr = months.split(",");
		for (String month : monthArr) {
			bpPerformanceService.initTaskOfTheMonths(bpPerformanceService.getCurrentYear(), Integer.valueOf(month));
		}

		logger.info("[BP]根据传入月份参数初始化月度task结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * 根据传入月份参数初始化季度task
	 */
	@RequestMapping(value = "/bp/task/trigger/quarters")
	public void triggerQuarterTaskByMonths(String months) {
		StopWatch stopWatch = new StopWatch();
		if(StringUtil.isBlank(months)) {
			logger.error("传入月份为空，任务结束");
			return;
		}

		String[] monthArr = months.split(",");
		for (String month : monthArr) {
			bpPerformanceService.initTaskOfTheQuarter(bpPerformanceService.getCurrentYear(), Integer.valueOf(month));
		}
		logger.info("[BP]根据传入月份参数初始化季度task结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * 初始化当前月的月度报表数据
	 */
	@RequestMapping(value = "/bp/task/trigger/month")
	public void triggerMonthTask() {
		StopWatch stopWatch = new StopWatch();
		reportHisTask.bpPerformanceReportPerMonth();
		logger.info("[BP]月度报表触发结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * 初始化当前季度的季度报表数据
	 */
	@RequestMapping(value = "/bp/task/trigger/quarter")
	public void triggerQuarterTask() {
		StopWatch stopWatch = new StopWatch();
		reportHisTask.bpPerformanceReportPerQuarter();
		logger.info("[BP]季度报表触发结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * BP报表发送
	 */
	@RequestMapping(value = "/bp/task/trigger/report")
	public void triggerBpReportSendTask() {
		StopWatch stopWatch = new StopWatch();
		bpPerformanceService.bpPerformanceReportSend();
		logger.info("[BP]季度报表发送结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}

	/**
	 * 生成报表
	 * @throws FileNotFoundException 模板未找到
	 */
	@RequestMapping(value = "/bp/task/trigger/generate")
	public void generateBpReport(int month) throws FileNotFoundException {
		StopWatch stopWatch = new StopWatch();
		int currMonth = bpPerformanceService.getCurrentMonth();
		if(month>=1 && month<=12) {
			currMonth = month;
		}
		bpPerformanceService.generateBpReport(currMonth);
		logger.info("[BP]BP报表生成结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
	}
}